<?php
/**
 * 商品类型控制器
 */
Class TypeAction extends CommonAction {

	/**
	 * 类型列表模版
	 */
	Public function index () {
		import('ORG.Util.Page');
		$db = M('type');
		//计算分页
		$count = $db->count('id');
		$page = new Page($count, 10);
		$limit = $page->firstRow . ',' . $page->listRows;
		$result = $db->limit($limit)->select();

		//分配置变量
		$this->type = $result;
		$this->page = $page->show();
		$this->display();
	}

	/**
	 * 添加类型页
	 */
	Public function addType () {
		$this->display();
	}

	/**
	 * 添加类型操作
	 */
	Public function runAddType () {
		$data = array(
			'name' => $this->_post('name')
			);
		if (M('type')->data($data)->add()) {
			$this->success('添加成功', U('index'));
		} else {
			$this->error('添加失败');
		}
	}

	/**
	 * 编辑商品类型视图
	 */
	Public function editType () {
		$i = isset($_GET['id']) ? 1 : 0;
		if ($i) {
			$id = (int) $_GET['id'];
			$where = array('id' => $id);
		    $edit = M('type')->where($where)->find();
			$this->id = $id;
			$this->edit = $edit;
		}
		$this->display();
	}


	/**
	 * 修改商品类型操作
	 */
	Public function runEditType () {
		// p($_POST);die;
		$data = array(
			'name' => $_POST['name']
			);
		$id = (int) $_POST['id'];
		$where = array('id' => $id);
		if (M('type')->where($where)->data($data)->save()) {
			$this->success('修改成功', U('index'));
		} else {
			$this->error('修改失败');
		}
	}

	/**
	 * 类型属性列表页
	 */
	Public function attrList () {
		import('ORG.Util.Page');
		$tid = (int) $_GET['id'];
		$where = array('tid' => $tid);
		$result = M('type_attr');
        $count = $result->where($where)->count('id');
        // echo $count;die;
        $page = new Page($count, 10);
        $limit = $page->firstRow . ',' . $page->listRows;
        $result = $result->where($where)->limit($limit)->select();
        // p($result);die;
		$this->attr = $result;
		$this->page = $page->show();
		$this->display();
	}

	/**
	 * 添加类型属性页面
	 */
	Public function addAttr() {
		$this->display();
	}

	/**
	 * 添加类型属性操作
	 */
	Public function runAddAttr () {
		$value = str_replace(PHP_EOL, ',', $_POST['value']);
		$tid = $this->_post('tid', 'intval');
		// echo "$tid";die;
		$data = array(
			'name' => $this->_post('name'),
			'value' => $value,
			'type' => $this->_post('type', 'intval'),
			'tid' => $tid
			);
		if (M('type_attr')->data($data)->add()) {
			$Type = M("Type"); // 实例化Type对象
            $Type->where("id=$tid")->setInc('attrnum'); // 类型的属性数加1
			$this->success('添加成功', U('attrList', array('id' => $_POST['tid'])));
		} else {
			$this->error('添加失败');
		}
	}


    /**
	 * 编辑商品类型属性视图
	 */
	Public function editAttr () {
		$i = isset($_GET['id']) ? 1 : 0;
		if ($i) {
			$id = (int) $_GET['id'];
			$where = array('id' => $id);
		    $edit = M('type_attr')->where($where)->find();
		    $edit['value'] = str_replace(',', PHP_EOL, $edit['value']);
			$this->id = $id;
			$this->edit = $edit;
		}
		$this->display();
	}


	/**
	 * 修改商品类型属性操作
	 */
	Public function runEditAttr () {
		$value = str_replace(PHP_EOL, ',', $_POST['value']);
		$tid = $this->_post('tid', 'intval');
		 // p($_POST);die;
		$data = array(
			'name' => $this->_post('name'),
			'value' => $value,
			'type' => $this->_post('type', 'intval'),
			);
		$id = (int) $_POST['id'];
		$where = array('id' => $id);
		if (M('type_attr')->where($where)->data($data)->save()) {
			$this->success('修改成功', U('attrList', array('id' => $tid)));
		} else {
			$this->error('修改失败');
		}
		
	}




















}
?>